#include<iostream>
#include<string>
using namespace std;
struct node // 定义一个链表结构体存放用户信息 姓名 电话
{
    string name;
    string telephone;
    node* next;
};
// node* ReadList();
node* FindbyId(node* head); // 通过姓名查找
node* FindbyTel(node* head); // 通过电话查找
node* readList(); // 录入用户信息
int main()
{
    // 获取链表信息
    cout<<"Enter 0 finish input"<<endl;
    node* head=readList();
    cout<<"which way do you want name or telephone?(N/T):"<<endl;
    char flag;
    cin>>flag;
    if (flag == 'n' || flag == 'N')
    {   
        node* name= FindbyId(head);
        if (name !=  NULL)
        {
            cout<<"name:"<<name->name<<endl;
            cout<<"Telephone:"<<name->telephone<<endl;
        }else {
            cout<<"Sorry we not find it."<<endl;
        }
        
    }
    if (flag == 't' || flag == 'T')
    {   
        node* tel= FindbyTel(head);
        if (tel !=  NULL)
        {
            cout<<"name:"<<tel->name<<endl;
            cout<<"Telephone:"<<tel->telephone<<endl;
        }else {
            cout<<"Sorry we not find it."<<endl;
        }
        
    }

    system("pause");
    
}
// 通过名字查找
node* FindbyId(node* head)
{
    cout<<"enter the name you want to look for:";
    string name;
    cin>>name;
    int name_number=name.length();
    node* p;
    p=head;
    while ( p->next != NULL)
    {
        if(p->name.substr(0,name_number) == name)
        {
            return p;
        }
        p=p->next;
    }
    return NULL;
}

// 通过电话查找
node* FindbyTel(node* head)
{
    cout<<"enter the telephone you want to look for:";
    string tel;
    cin>>tel;
    int tel_number=tel.length();
    node* p;
    p=head;
    while ( p->next != NULL)
    {
        if(p->telephone.substr(0,tel_number) == tel)
        {
            return p;
        }
        p=p->next;
    }
    return NULL;
}

// 用户录入信息
node* readList()
{
    node* head=new node;
    node* p=new node;
    head=p;
    while (true)
    {
        cout<<"Please enter the person name:";
        cin>>p->name;
        if(p->name=="0") break;
        cout<<"Please enter the person telphone:";
        cin>>p->telephone;
        node* pr=new node;
        pr->next=NULL;
        p->next=pr;
        p=pr;
    }
    return head; 
}